package com.alen.sun.service.Impl;

import com.alen.sun.bean.Admin;
import com.alen.sun.bean.CheckIn;
import com.alen.sun.bean.Elderly;
import com.alen.sun.bean.PageBean;
import com.alen.sun.dao.CheckInMapper;
import com.alen.sun.service.CheckInService;
import com.alen.sun.util.PhoneCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
@Transactional
public class CheckInServiceImpl implements CheckInService {
    @Autowired
    private CheckInMapper checkInMapper;

    /**
     * 添加
     * @param checkIn
     * @return
     */
    @Override
    public Boolean addCheckIn(CheckIn checkIn) {
        log.info("getEName"+checkIn.getName());
        log.info("getEGender"+checkIn.getGender());
        log.info("getEPhoneNum"+checkIn.getPhoneNum());
        log.info("getENote"+checkIn.getNote());
        log.info("getEAge"+checkIn.getAge());
        log.info("getAddress"+checkIn.getAddress());
        log.info("getAcceptedOrNot"+checkIn.getAcceptedOrNot());
        log.info("getAcceptTime"+checkIn.getAcceptTime());
        log.info("getAcceptStatus"+checkIn.getAcceptStatus());
        log.info("getFolkPhoneNum"+checkIn.getFolkPhoneNum());
        //先判断必要参数不为空
        if(checkIn.getName()!=null && !checkIn.getName().isEmpty()
        && checkIn.getGender()!=null && !checkIn.getGender().isEmpty()
        && checkIn.getPhoneNum()!=null && !checkIn.getPhoneNum().isEmpty()
        && checkIn.getAge()!=null && !checkIn.getAge().equals(0)
        && checkIn.getAddress()!=null && !checkIn.getAddress().isEmpty()
        && checkIn.getAcceptedOrNot()!=null
        && checkIn.getAcceptTime()!=null && !checkIn.getAcceptTime().equals(new Date(0))
        && checkIn.getAcceptStatus()!=null
        && checkIn.getFolkPhoneNum()!=null && !checkIn.getFolkPhoneNum().isEmpty()){
        log.info("打印信息"+checkIn.getName()+"老人电话"+checkIn.getPhoneNum()+"家属电话"+checkIn.getFolkPhoneNum());
        log.info(checkIn.getName(),checkIn.getGender(),checkIn.getPhoneNum(),checkIn.getNote(),checkIn.getAge(),checkIn.getAddress(),checkIn.getAcceptedOrNot(),checkIn.getAcceptTime(),checkIn.getAcceptStatus(),checkIn.getFolkPhoneNum());
        //添加之前先判断是否存在 ename,ephone,folkPhoneNum
            Integer count = checkInMapper.selectRecord(checkIn);
            if(count == null || count == 0){
                //不存在一样的
                //生成id
                PhoneCode phoneCode = new PhoneCode();
                while (true){
                    String checkInId = phoneCode.generateCode();
                    //查询id是否重复
                    Integer count1 = checkInMapper.selectById(checkInId);
                    if(count1 == null || count1 ==0){
                        //不存在重复
                        checkIn.setCheckInId(checkInId);
                        //跳出循环
                        break;
                    }
                }
                //执行添加操作
                Boolean flag = checkInMapper.addCheckIn(checkIn);
                return flag;
            }
        }
        return false;
    }

    /**
     * 修改
     * @param checkIn
     * @return
     */
    @Override
    public Boolean updateCheckIn(CheckIn checkIn) {
        //先判断必要参数不为空
        if(checkIn.getName()!=null && !checkIn.getName().isEmpty()
                && checkIn.getGender()!=null && !checkIn.getGender().isEmpty()
                && checkIn.getPhoneNum()!=null && !checkIn.getPhoneNum().isEmpty()
                && checkIn.getAge()!=null && !checkIn.getAge().equals(0)
                && checkIn.getAddress()!=null && !checkIn.getAddress().isEmpty()
                && checkIn.getAcceptedOrNot()!=null
                && checkIn.getAcceptTime()!=null && !checkIn.getAcceptTime().equals(new Date(0))
                && checkIn.getAcceptStatus()!=null
                && checkIn.getFolkPhoneNum()!=null && !checkIn.getFolkPhoneNum().isEmpty()){
            Boolean flag = checkInMapper.updateCheckIn(checkIn);
            return flag;
        }
            return false;
    }

    /**
     * 删除
     * @param checkIn
     * @return
     */
    @Override
    public Boolean deleteCheckIn(CheckIn checkIn) {
        Boolean flag = checkInMapper.deleteCheckIn(checkIn);
        return flag;
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageBean page(Integer page, Integer pageSize) {
        //1. 设置分页参数
        //计算参数start
        Integer start = (page-1)*pageSize;
        List<CheckIn> checkInList = checkInMapper.page(start, pageSize);
        //2.查询总数
        Long count = checkInMapper.count();
        PageBean pageBean = new PageBean(count,checkInList);
        return pageBean;
    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    public Boolean deleteByIds(List<String> ids) {
        Boolean flag = checkInMapper.deleteByIds(ids);
        return flag;
    }

    /**
     * 聚合搜索,网页搜索功能
     * @param keywords
     * @return
     */
    @Override
    public List<CheckIn> searchByKeywords(String keywords) {
        List<CheckIn> checkIns = checkInMapper.searchByKeywords(keywords);
        return checkIns;
    }

    /**
     * 数据分析可视化,获取入住日期及入住数量
     * @return
     */
    @Override
    public List<Map<String, Object>> getCountByAcceptTime() {
        List<Map<String, Object>> resultList = checkInMapper.getCountByAcceptTime();
        return resultList;
    }

    /**
     * 成功办理入住
     * @param checkIn
     * @return
     */
    @Override
    public Boolean setStatusByCheckInId(CheckIn checkIn) {
        Boolean flag = checkInMapper.setStatusByCheckInId(checkIn);
        return flag;
    }

    /**
     * 自动添加老人信息
     * @param checkIn
     * @return
     */
    @Override
    public Elderly autoAddElderly(CheckIn checkIn) {
        Elderly elderly = new Elderly();
        elderly.setName(checkIn.getName());//姓名
        elderly.setGender(checkIn.getGender());//性别
        elderly.setPhoneNum(checkIn.getPhoneNum());//电话
        elderly.setAge(checkIn.getAge());//年龄
        elderly.setNote(checkIn.getNote());//备注
        elderly.setEmployeeId("000000");//默认护工
        elderly.setPassword("123456");//默认密码

        return elderly;
    }
}
